package com.cheyun.netsalev3.repository.aftersale

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.aftersale.CarOwnerData
import com.cheyun.netsalev3.bean.aftersale.CarOwnerParam
import com.cheyun.netsalev3.mock.AfterSale
import com.cheyun.netsalev3.repository.BasePageRepository
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class CarOwnerRepository(var map: Map<String, String> = HashMap(),var enable:Boolean = false) : BasePageRepository<CarOwnerData>() {
    fun setEanbleLoad(e:Boolean){
        this.enable = e
    }
    fun changeMap(newMap:HashMap<String, String>){
        this.map = newMap
    }
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<CarOwnerData>) -> Unit) {
        if(!this.enable)return
        ApiClient.getInstance().getCarOwnerList(1,map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<CarOwnerParam>>() {
                    override fun onSuccess(t: BaseResp<CarOwnerParam>) {
                        total.postValue(t.data!!.total)
                        t.data?.apply {
                            callback(this.rows)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, CarOwnerData>) {
        if(!this.enable)return
        ApiClient.getInstance().getCarOwnerList(params.key,map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<CarOwnerParam>>() {
                    override fun onSuccess(t: BaseResp<CarOwnerParam>) {
                        t.data?.apply {
                            callback.onResult(this.rows, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {
                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, CarOwnerData>) {
        if(!this.enable)return
        ApiClient.getInstance().getCarOwnerList(params.key,map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<CarOwnerParam>>() {
                    override fun onSuccess(t: BaseResp<CarOwnerParam>) {
                        t.data?.apply {
                            callback.onResult(this.rows, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {
                    }


                })
    }
}